﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;

namespace RH
{
    public partial class wfIngresoSubsidioMnt : EditPage
    {
        public const int _GRDLIST_STCUOTA = 11;
        public const int _GRDLIST_STANULADO = 12;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                this.LoadInPostBack();
                return;
            }
            this.LoadData();
        }

        public override void LoadInPostBack()
        {
            this.RegisterGeneralScripts();
        }

        public override void LoadInitialSettings()
        {
            this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
            this.SetInitialTitle("Subsidio");
        }

        public override void LoadListControls()
        {
            ETablaGenDet objEDet = new ETablaGenDet();
            MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

            objEDet.IdTabla = ConstMultitabla.TIPO_SUBSIDIO;
            DataTable dt = objBOI.GetDetailByList(objEDet);
            WebHelper.LoadListControl(ddlidTipoSubsidio, dt, "dsDescrip", "idCodigo", true, false);

            this.hdnstCalcular.Value = ConstFlagEstado.DESACTIVADO;
            this.hdnstNuProceCal.Value = ConstFlagEstado.DESACTIVADO;

            if (this.FormEditAction == FormEditAction.AddNew)
                this.txtfeSubsidio.Text = DateTime.Now.ToString();
        }

        public override void DataToControls()
        {
            ESubsidio objE = new ESubsidio();
            IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Subsidio));
            objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objE.IdPersonal = Request.QueryString["IdPersonal"];
            objE.IdSubsidio = Request.QueryString["IdSubsidio"];
            objE = (ESubsidio)objBO.GetByKey(objE);

            if (objE != null)
            {
                WebHelper.EntityToControl(objE, "IdPersonal", hdnidPersonal);
                WebHelper.EntityToControl(objE, "DsObserva", txtdsObservacion);
                EPersonal objEPer = new EPersonal();
                RHBOQry.IPersonal objBOPer = (RHBOQry.IPersonal)WCFHelper.GetObject<RHBOQry.IPersonal>(typeof(RHBOQry.Personal));
                objEPer.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                objEPer.IdPersonal = objE.IdPersonal;
                DataTable dtPer = objBOPer.GetCliProvXPersonal(objEPer);
                if (dtPer.Rows.Count > 0)
                    this.txtidPersonal.Text = dtPer.Rows[0]["dsRazSocial"].ToString();

                WebHelper.EntityToControl(objE, "FeSubsidio", txtfeSubsidio);
                WebHelper.EntityToControl(objE, "FeInicioPerioIncapa", txtfeInicio);
                WebHelper.EntityToControl(objE, "FeFinPerioIncapa", txtfeFin);
                WebHelper.EntityToControl(objE, "NuProcAnual", txtnuProcAnual);

                ETablaGenDet objEDet = new ETablaGenDet();
                IBOQuery objBOI = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.TablaGenDet));

                objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
                objEDet.IdCodigo = objE.IdForPago;
                objEDet = (ETablaGenDet)objBOI.GetByKey(objEDet);


                WebHelper.EntityToControl(objE, "IdForPago", hdnidForPago);
                if (objEDet!= null)
                    WebHelper.EntityToControl(objEDet, "DsDescrip", txtidForPago);

                EPlanilla objEPl = new EPlanilla();
                IBOQuery objBOPl = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Planilla));
                objEPl.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                objEPl.IdPlanilla = objE.IdPlanilla;

                objEPl = (EPlanilla)objBOPl.GetByKey(objEPl);
                if (objEPl != null)
                    WebHelper.EntityToControl(objEPl, "DsPlanilla", txtidPlanilla);


                WebHelper.EntityToControl(objE, "IdTipoSubsidio", ddlidTipoSubsidio);
                
            }

           
        }

        public override void RegisterGeneralScripts()
        {
            this.txtidSubsidio.Attributes.Add("readonly", "true");
            this.txtidPlanilla.Attributes.Add("readonly", "true");
            this.txtidForPago.Attributes.Add("readonly", "true");
            this.txtnuProcAnual.Attributes.Add("readonly", "true");

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("var varRet = modalDialog('wfPeriodoPlani.aspx?IdPersonal=' + document.getElementById('" + this.hdnidPersonal.ClientID + "').value,750,400);");
            sb.AppendLine("if(varRet==null)");
            sb.AppendLine("return false;");
            sb.AppendLine("ctl = document.getElementById('" + this.hdnidForPago.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[0];");
            sb.AppendLine("ctl = document.getElementById('" + this.txtidForPago.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[1];");
            sb.AppendLine("ctl = document.getElementById('" + this.hdnidPlanilla.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[2];");
            sb.AppendLine("ctl = document.getElementById('" + this.txtidPlanilla.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[3];");
            sb.AppendLine("ctl = document.getElementById('" + this.txtnuProcAnual.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[4];");

            this.txtidPlanilla.OnClientClick = sb.ToString();
            
            sb = new StringBuilder();
            sb.AppendLine("var varRet = modalDialog('wfPersonal.aspx',750,400);");
            sb.AppendLine("if(varRet==null)");
            sb.AppendLine("return false;");
            sb.AppendLine("ctl = document.getElementById('" + this.hdnidPersonal.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[0];");
            sb.AppendLine("ctl = document.getElementById('" + this.txtidPersonal.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[1];");
            this.txtidPersonal.OnClientClick = sb.ToString();

            sb = new StringBuilder();

            sb.AppendLine("function validateEditForm(key){");
            sb.AppendLine("var varRpta = isEmptyControl('" + this.txtidPersonal.ClientID + "','Ingrese el Personal');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidTipoSubsidio.ClientID + "','Seleccione el Tipo de Subsidio');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("var varRpta = isEmptyControl('" + this.txtfeSubsidio.ClientID + "','Ingrese la fecha de Subsidio');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");

            sb.AppendLine("var varRpta = isEmptyControl('" + this.txtfeInicio.ClientID + "','Ingrese la fecha de Inicio del Periodo de Incapacidad');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");

            sb.AppendLine("var varRpta = isEmptyControl('" + this.txtfeFin.ClientID + "','Ingrese la fecha de Fin del Periodo de Incapacidad');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");

            sb.AppendLine("varRpta = isEmptyControl('" + this.txtidPlanilla.ClientID + "','Seleccione la planilla.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuProcAnual.ClientID + "','Ingrese el Nro de Proceso Inicial.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
            sb.AppendLine("}");

            ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
        }

        protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
        {
            if (e.Item == null)
                return;

            switch (e.Item.Value)
            {
                case ConstMenuToolbarValue.BUTTON_SAVE:
                case ConstMenuToolbarValue.BUTTON_SAVENEW:
                    if (this.FormEditAction != FormEditAction.Delete)
                        this.SaveData(e.Item.Value);
                    else
                        this.DeleteData();
                    break;
                case ConstMenuToolbarValue.BUTTON_EXIT:
                    this.Response.Redirect("wfIngresoSubsidioBnd.aspx");
                    break;
            }
        }
         

        public override void SaveData(String key)
        {
            if (this.ValidateData() == false)
                return;

            IEntityBase objE = (IEntityBase)this.CreateEntitySet();
            IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Subsidio));

            String[] strRet = objPro.UpdateData(objE);
            String strMsg = "";

            switch (this.FormEditAction)
            {

                case FormEditAction.AddNew:
                case FormEditAction.Copy:
                    strMsg = "Se ha registrado el Subsidio : " + strRet[0].ToString();
                    break;
                default:
                    strMsg = "Se ha modificado el Subsidio : " + this.txtidSubsidio.Text;
                    break;
            }
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfIngresoSubsidioBnd.aspx");
        }

        public override void DeleteData()
        {
            IEntityBase objE = (IEntityBase)this.CreateEntitySet();
            IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Subsidio));
            objPro.DeleteData(objE);

            //this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "Se anulo el derechohabiente  : " + txtidPersonal.Text, "wfPerDereHabiBnd.aspx");

        }

        public override Object CreateEntitySet()
        {
            ESubsidio objE = new ESubsidio();

            if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
            {
                objE.EntityState = EntityState.Added;
                objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
                objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                WebHelper.ControlToEntity(hdnidPersonal, objE, "IdPersonal");
                WebHelper.ControlToEntity(txtidSubsidio, objE, "IdSubsidio");
                WebHelper.ControlToEntity(ddlidTipoSubsidio, objE, "IdTipoSubsidio");
                WebHelper.ControlToEntity(txtfeSubsidio, objE, "FeSubsidio");
                WebHelper.ControlToEntity(txtfeInicio, objE, "FeInicioPerioIncapa");
                WebHelper.ControlToEntity(txtfeFin, objE, "FeFinPerioIncapa");
                WebHelper.ControlToEntity(hdnidPlanilla, objE, "IdPlanilla");
                WebHelper.ControlToEntity(hdnidForPago, objE, "IdForPago");
                WebHelper.ControlToEntity(txtnuProcAnual, objE, "NuProcAnual");
                WebHelper.ControlToEntity(txtdsObservacion, objE, "DsObserva");
            }
            else
            {
                IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Subsidio));
                objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
                objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                objE.IdPersonal = Request.QueryString["IdPersonal"];
                objE.IdSubsidio = Request.QueryString["IdSubsidio"];
                objE = (ESubsidio)objBO.GetByKey(objE);

                if (this.FormEditAction == FormEditAction.Edit)
                    objE.EntityState = EntityState.Modified;
                if (this.FormEditAction == FormEditAction.Delete)
                    objE.EntityState = EntityState.Deleted;
                WebHelper.ControlToEntity(ddlidTipoSubsidio, objE, "IdTipoSubsidio");
                WebHelper.ControlToEntity(txtfeSubsidio, objE, "FeSubsidio");
                WebHelper.ControlToEntity(txtfeInicio, objE, "FeInicioPerioIncapa");
                WebHelper.ControlToEntity(txtfeFin, objE, "FeFinPerioIncapa");
                WebHelper.ControlToEntity(txtdsObservacion, objE, "DsObserva");


            }

            objE.StAnulado = ConstFlagEstado.DESACTIVADO;

            return objE;
        }

        public override void LockControls()
        {
            if (this.FormEditAction != FormEditAction.AddNew)
            {
                this.txtidSubsidio.Enabled = false;
                this.txtidPersonal.EnabledButton = false;
                this.txtidPlanilla.EnabledButton = false;
                //this.ddlidMoneda.Enabled = false;
            }
        }

    }
}